package com.example.springmvcproject.Service;

import cn.dev33.satoken.stp.StpUtil;
import com.example.springmvcproject.Mapper.BankMapper;
import com.example.springmvcproject.Pojo.Bank;
import com.example.springmvcproject.Service.Inteface.BankService;
import com.example.springmvcproject.Utils.ResultCode;
import com.example.springmvcproject.Utils.ResultMessage;
import com.example.springmvcproject.Vo.BankSelectiveVo;
import com.example.springmvcproject.Vo.BankVo;
import com.example.springmvcproject.Vo.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class BankServiceImpl implements BankService {

    @Resource
    BankMapper bankMapper;


    /**
     * 添加一个银行卡
     * @param bankVo
     * @return
     */
    @Override
    public ResultMessage addBank(BankVo bankVo) {
        //获取指定登录ID
        Integer userId = StpUtil.getLoginIdAsInt();
        //通过用户ID名称获取指定的Bank判断是否有内容
        Bank bank=bankMapper.selectBankByUserIdAndBankName(new Bank(null,userId, bankVo.getBankName()));
        if (bank!=null) {
            return new ResultMessage(ResultCode.RESULT_PARAMS.getCode(), ResultCode.RESULT_PARAMS.getStatus(), "您输入的银行名称已存在",null);
        }
        Bank tempBank = new Bank(null,userId, bankVo.getBankName());
        bankMapper.insertBank(tempBank);
        return new ResultMessage(ResultCode.RESULT_OK.getCode(), ResultCode.RESULT_OK.getStatus(), null,"添加成功");
    }

    /**
     * 通过银行名称搜索银行
     *
     * @param bankSelectiveVo
     * @return
     */
    @Override
    public ResultMessage selectBankByBankName(BankSelectiveVo bankSelectiveVo) {
        //获取当前登录的user_id
        Integer userId=StpUtil.getLoginIdAsInt();
        //分页插件
        Page<Object> objects = PageHelper.startPage(bankSelectiveVo.getPageIndex(), bankSelectiveVo.getPageSize());
        //搜索数据
        String searchText= bankSelectiveVo.getSearchText().trim().length()==0?null:bankSelectiveVo.getSearchText();
        List<Bank> data= bankMapper.selectBankByBankName(new Bank(null,userId,searchText));

        return new ResultMessage(ResultCode.RESULT_OK.getCode(),
                ResultCode.RESULT_OK.getStatus(),
                null,
                new PageResult(objects.getTotal(),objects.getPageNum(),objects.getPageSize(),data));
    }

    /**
     * 根据当前登录ID查找所属的所有银行卡
     *
     * @return
     */
    @Override
    public ResultMessage selectBanksByUserId() {
        //获取登录ID
        Integer userId=StpUtil.getLoginIdAsInt();
        //传递数据库进行查询
        List<Bank> data = bankMapper.selectBankByBankName(new Bank(null,userId,null));

        return new ResultMessage(ResultCode.RESULT_OK.getCode(), ResultCode.RESULT_OK.getStatus(), null,data);
    }

    /**
     * 根据ID修改指定银行名称
     *
     * @param bankVo
     * @return
     */
    @Override
    public ResultMessage updateBankNameById(BankVo bankVo) {
        //获取用户ID
        Integer userId=StpUtil.getLoginIdAsInt();
        boolean flag=bankMapper.updateBankNameById(new Bank(bankVo.getBankId(),userId, bankVo.getBankName()));
        if (!flag){
            return new ResultMessage(ResultCode.RESULT_NULL.getCode(), ResultCode.RESULT_NULL.getStatus(), "您传递的银行ID并不属于您",null);
        }
        return new ResultMessage(ResultCode.RESULT_OK.getCode(), ResultCode.RESULT_OK.getStatus(), null,"修改成功");
    }

    /**
     * 通过ID名称删除银行卡
     *
     * @param bankVo
     * @return
     */
    @Override
    public ResultMessage deleteBankByIdAndName(BankVo bankVo) {
        //获取用户ID
        Integer userId=StpUtil.getLoginIdAsInt();
        //数据丢进数据库
        boolean flag=bankMapper.deleteBankByIdAndName(new Bank(bankVo.getBankId(),userId, bankVo.getBankName()));
        if (!flag) return new ResultMessage(ResultCode.RESULT_NULL.getCode(), ResultCode.RESULT_NULL.getStatus(), "非法传参",null);
        return new ResultMessage(ResultCode.RESULT_OK.getCode(), ResultCode.RESULT_OK.getStatus(), null,"删除成功");
    }

}
